package com.whjz.dcfs.jzxx.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.whjz.dcfs.jzxx.dao.IDcfsDocmentDAO;
import com.whjz.dcfs.jzxx.model.DcfsDocment;
import com.whjz.dcfs.util.BaseAction;

public class DcfsDocumentAction extends BaseAction{

	private static final long serialVersionUID = -5696813892828553218L;

	private DcfsDocment dcfsDocument;
	@Autowired
	private IDcfsDocmentDAO dcfsDocumentDAO;
	
	private File uploadFile;
	
	private String uploadFileFileName;
	
	private String uploadFileContentType;
    private String FiledataFileName;//附件名字
    private String FiledataContentType;//附件类型
    private InputStream downLoadFile;//导出文件的输入流
	private String path;
	private String docUrl;
	
	public void insertDcfsDocmentByCond() throws Exception{
		getHttpServletResponse().setCharacterEncoding("utf-8");
		InputStream is=new FileInputStream(uploadFile);
		createDir(path);
		String fileEnd=uploadFileFileName.substring(uploadFileFileName.lastIndexOf("."), uploadFileFileName.length());
		String realFileName=String.valueOf(System.currentTimeMillis())+fileEnd;
		File toFile=new File(path,realFileName);
		OutputStream os=new FileOutputStream(toFile);
		byte buffer[] =new byte[8192];
		int count=0;
		while((count=is.read(buffer))>0){
			os.write(buffer, 0, count);
		}
		String localhost=ServletActionContext.getRequest().getServerName();
		String port=String.valueOf(ServletActionContext.getRequest().getServerPort());
		path=path.substring(3, path.length());
		os.close();
		is.close();
		dcfsDocument.setDocurl("http:\\\\"+localhost+":"+port+"\\"+path+"\\"+realFileName);
		dcfsDocument.setDocid(String.valueOf(System.currentTimeMillis()));
		dcfsDocumentDAO.insertDcfsDocmentByCond(dcfsDocument);
		getHttpServletResponse().getWriter().println("{success:'true',msg:'添加成功'}");
	}
	
	public void getDcfsDocmentListByCond() throws Exception{
		getHttpServletResponse().setCharacterEncoding("utf-8");
		List<DcfsDocment> list=dcfsDocumentDAO.getDcfsDocmentListByCond(dcfsDocument);
		StringBuffer sb=new StringBuffer();
		sb.append("{wd:[");
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				sb.append("{");
				sb.append("docid:'"+(list.get(i).getDocid()==null?"":list.get(i).getDocid())+"',");
				sb.append("jzsbbh:'"+(list.get(i).getJzsbbh()==null?"":list.get(i).getJzsbbh())+"',");
				sb.append("docname:'"+(list.get(i).getDocname()==null?"":list.get(i).getDocname())+"',");
				sb.append("doctime:'"+(list.get(i).getDoctime()==null?"":list.get(i).getDoctime())+"',");
				sb.append("docauthor:'"+(list.get(i).getDocauthor()==null?"":list.get(i).getDocauthor())+"',");
				sb.append("doctype:'"+(list.get(i).getDoctype()==null?"":list.get(i).getDoctype())+"',");
				String docurl=list.get(i).getDocurl().replace("\\", "\\\\");
				sb.append("docurl:'"+(docurl)+"'");
				sb.append("}");
				if(i<list.size()-1){
					sb.append(",");
				}
			}
		}
		sb.append("]}");
		getHttpServletResponse().getWriter().print(sb);
	}
	
	public void deleteDcfsDocmentByCond() throws Exception{
//		System.out.println(path);
		getHttpServletResponse().setCharacterEncoding("utf-8");
		String[] docids=dcfsDocument.getDocid().split(",");
		List<DcfsDocment> list=new ArrayList<DcfsDocment>();
		DcfsDocment object;
		for (String string : docids) {
			object=new DcfsDocment();
			object.setDocid(string);
			list.add(object);
		}
//		dcfsDocumentDAO.deleteDcfsDocmentByCond(dcfsDocument);
		dcfsDocumentDAO.deleteDcfsDocmentListByCond(list);
		String[] urls=dcfsDocument.getDocurl().split(",");
		File file;
		String name;
		for (String string : urls) {
			name=string.split("\\\\")[4];
			file=new File(path+"/"+name);
//			删除文件
		if(file!=null){
		if(file.exists()){
			file.delete();
		}
		}
		}
		getHttpServletResponse().getWriter().println("{success:'true',msg:'删除成功'}");
	}
	
	public static boolean createDir(String name){
		File dir=new File(name);
		if(dir.exists()){
			return false;
		}
		if(name.endsWith(File.separator))
			name=name+File.separator;
		if(dir.mkdirs()){
			return true;
		}else{
			return false;
		}
	}
	/***
	 * 下载附件
	 * */
	public String downLoadFile(){
			try {
				FiledataFileName = new String(FiledataFileName.getBytes(),"ISO-8859-1");
				String[] str=docUrl.split("\\\\");
				String[] str2=docUrl.split("\\.");
				FiledataFileName=FiledataFileName+"."+str2[str2.length-1];
				downLoadFile=new FileInputStream(new File(path+File.separator+str[str.length-1]));
			} catch (Exception e) {
				e.printStackTrace();
			}
		return "downLoad";
	}
	public DcfsDocment getDcfsDocument() {
		return dcfsDocument;
	}
	public void setDcfsDocument(DcfsDocment dcfsDocument) {
		this.dcfsDocument = dcfsDocument;
	}
	public File getUploadFile() {
		return uploadFile;
	}
	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}
	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}

	public String getFiledataFileName() {
		return FiledataFileName;
	}

	public void setFiledataFileName(String filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public String getFiledataContentType() {
		return FiledataContentType;
	}

	public void setFiledataContentType(String filedataContentType) {
		FiledataContentType = filedataContentType;
	}

	public InputStream getDownLoadFile() {
		return downLoadFile;
	}

	public void setDownLoadFile(InputStream downLoadFile) {
		this.downLoadFile = downLoadFile;
	}

	public String getDocUrl() {
		return docUrl;
	}

	public void setDocUrl(String docUrl) {
		this.docUrl = docUrl;
	}
	
	
}
